package com.ruoyi.common.h1wUtils;

import com.ruoyi.common.core.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;


@RestController
public class CommonUtils extends BaseController {

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 获取各种编号
     */
    public String makeCode(String prefix){   //prefix(2) + YYYYMMddHH(8) + 计数器(6)
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHH");
        String key = prefix + dateFormat.format(new Date());
        if(!redisUtils.hasKey(key)){
            redisUtils.set(key,1,1, TimeUnit.DAYS);
        }
        System.out.println(redisUtils.get(key));
        StringBuffer code = new StringBuffer();
        code.append(prefix);//前缀
        code.append(dateFormat.format(new Date()));
        code.append(String.format("%06d", redisUtils.incr(key,1)));  //计数器
        return code.toString();
    }
}
